<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yoursite.com","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"flat"},"back2top":{"enable":true,"sidebar":true,"scrollpercent":true},"bookmark":{"enable":true,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="JS点击事件的绑定12345678&lt;input type&#x3D;&quot;button&quot; value&#x3D;&quot;按钮&quot; id&#x3D;&quot;btn&quot;&gt;    &lt;script&gt;    var btn &#x3D; document.getElementById(&#39;btn&#39;);    &#x2F;&#x2F;var btn &#x3D; document.getElementsByTagName(&#39;input&#39;)[0];    btn.onclick">
<meta property="og:type" content="article">
<meta property="og:title" content="前端&#x2F;实例">
<meta property="og:url" content="http://yoursite.com/2020/04/13/%E5%89%8D%E7%AB%AF/%E5%AE%9E%E4%BE%8B/index.html">
<meta property="og:site_name" content="heweiliang blog">
<meta property="og:description" content="JS点击事件的绑定12345678&lt;input type&#x3D;&quot;button&quot; value&#x3D;&quot;按钮&quot; id&#x3D;&quot;btn&quot;&gt;    &lt;script&gt;    var btn &#x3D; document.getElementById(&#39;btn&#39;);    &#x2F;&#x2F;var btn &#x3D; document.getElementsByTagName(&#39;input&#39;)[0];    btn.onclick">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-04-13T09:27:16.263Z">
<meta property="article:modified_time" content="2020-06-26T07:26:09.245Z">
<meta property="article:author" content="heweiliang">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://yoursite.com/2020/04/13/%E5%89%8D%E7%AB%AF/%E5%AE%9E%E4%BE%8B/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>前端/实例 | heweiliang blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="heweiliang blog" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">heweiliang blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-fw fa-user"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
        <li class="menu-item menu-item-schedule">

    <a href="/schedule/" rel="section"><i class="fa fa-fw fa-calendar"></i>日程表</a>

  </li>
        <li class="menu-item menu-item-sitemap">

    <a href="/sitemap.xml" rel="section"><i class="fa fa-fw fa-sitemap"></i>站点地图</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="reading-progress-bar"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

  <a href="https://github.com/yourname" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/04/13/%E5%89%8D%E7%AB%AF/%E5%AE%9E%E4%BE%8B/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="heweiliang">
      <meta itemprop="description" content="heweiliang blog">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="heweiliang blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          前端/实例<a href="https://github.com/user-name/repo-name/tree/branch-name/subdirectory-name_posts/%E5%89%8D%E7%AB%AF/%E5%AE%9E%E4%BE%8B.md" class="post-edit-link" title="编辑" rel="noopener" target="_blank"><i class="fa fa-pencil"></i></a>
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-04-13 17:27:16" itemprop="dateCreated datePublished" datetime="2020-04-13T17:27:16+08:00">2020-04-13</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-06-26 15:26:09" itemprop="dateModified" datetime="2020-06-26T15:26:09+08:00">2020-06-26</time>
              </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span><br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>9.3k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>8 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="JS点击事件的绑定"><a href="#JS点击事件的绑定" class="headerlink" title="JS点击事件的绑定"></a>JS点击事件的绑定</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&lt;input type=<span class="string">"button"</span> value=<span class="string">"按钮"</span> id=<span class="string">"btn"</span>&gt;</span><br><span class="line">    &lt;script&gt;</span><br><span class="line">    <span class="keyword">var</span> btn = <span class="built_in">document</span>.getElementById(<span class="string">'btn'</span>);</span><br><span class="line">    <span class="comment">//var btn = document.getElementsByTagName('input')[0];</span></span><br><span class="line">    btn.onclick = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">        alert(<span class="string">"你点击了按钮"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&lt;<span class="regexp">/script&gt;</span></span><br></pre></td></tr></table></figure>

<p>注意：<strong>onclick</strong>不是click或者onclick()</p>
<h1 id="JS语法实例"><a href="#JS语法实例" class="headerlink" title="JS语法实例"></a>JS语法实例</h1><h3 id="js控制按钮内容相互切换"><a href="#js控制按钮内容相互切换" class="headerlink" title="js控制按钮内容相互切换"></a>js控制按钮内容相互切换</h3><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">"en"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">meta</span> <span class="attr">name</span>=<span class="string">"viewport"</span> <span class="attr">content</span>=<span class="string">"width=device-width, initial-scale=1.0"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">meta</span> <span class="attr">http-equiv</span>=<span class="string">"X-UA-Compatible"</span> <span class="attr">content</span>=<span class="string">"ie=edge"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">title</span>&gt;</span>Document<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">            <span class="built_in">window</span>.onload = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="actionscript">                <span class="keyword">var</span> click_num = <span class="number">0</span>;</span></span><br><span class="line"><span class="javascript">                <span class="keyword">var</span> box = <span class="built_in">document</span>.getElementById(<span class="string">"box"</span>);</span></span><br><span class="line"><span class="javascript">                <span class="keyword">var</span> inp = <span class="built_in">document</span>.getElementsByTagName(<span class="string">"input"</span>)[<span class="number">0</span>];</span></span><br><span class="line"><span class="actionscript">                inp.onclick = <span class="function"><span class="keyword">function</span> <span class="params">()</span> </span>&#123;</span></span><br><span class="line">                    if (click_num % 2 == 0) &#123;</span><br><span class="line"><span class="actionscript">                        box.innerHTML = <span class="string">"内容1"</span>;</span></span><br><span class="line"><span class="actionscript">                    &#125; <span class="keyword">else</span> &#123;</span></span><br><span class="line"><span class="actionscript">                        box.innerHTML = <span class="string">"内容2"</span>;</span></span><br><span class="line">                    &#125;</span><br><span class="line">                    click_num++;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        <span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"box"</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"改变内容"</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>



<h3 id="JS打印时间"><a href="#JS打印时间" class="headerlink" title="JS打印时间"></a>JS打印时间</h3><p><em>toLocaleString());//转化为本地时间</em></p>
<p><em>date.getTime());//获取从1970年1月1日午夜零时，到当前时间的毫秒值</em></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">&lt;input type=<span class="string">"button"</span> value=<span class="string">"关闭时间"</span> id=<span class="string">"btn"</span>&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">    <span class="keyword">var</span> btn = <span class="built_in">document</span>.getElementById(<span class="string">'btn'</span>);</span><br><span class="line">    <span class="keyword">var</span> close_time = setInterval(Clock, <span class="number">1000</span>); <span class="comment">//定时器1s循环</span></span><br><span class="line">    btn.onclick = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">        alert(<span class="string">"你停止了时间！！！"</span>);</span><br><span class="line">        clearInterval(close_time);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">Clock</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="keyword">var</span> date = <span class="keyword">new</span> <span class="built_in">Date</span>();</span><br><span class="line">        <span class="keyword">var</span> year = date.getFullYear();</span><br><span class="line">        <span class="keyword">var</span> month = date.getMonth() + <span class="number">1</span>; <span class="comment">//显示月份 0-11，需要加1</span></span><br><span class="line">        <span class="keyword">var</span> day = date.getDate();x</span><br><span class="line">        <span class="keyword">var</span> hours = date.getHours();</span><br><span class="line">        <span class="keyword">var</span> minutes = date.getMinutes();</span><br><span class="line">        <span class="keyword">var</span> seconds = date.getSeconds();</span><br><span class="line">        <span class="comment">//var Milliseconds = date.getMilliseconds() 取当前的毫秒数</span></span><br><span class="line">        <span class="comment">//月份的显示为两位数字如09月</span></span><br><span class="line">        <span class="keyword">if</span> (month &lt; <span class="number">10</span>) &#123;</span><br><span class="line">            month = <span class="string">"0"</span> + month;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (date &lt; <span class="number">10</span>) &#123;</span><br><span class="line">            date = <span class="string">"0"</span> + date;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//时间拼接</span></span><br><span class="line">        <span class="keyword">var</span> dateTime = year + <span class="string">"年"</span> + month + <span class="string">"月"</span> + day + <span class="string">"日"</span> + hours + <span class="string">"时"</span> + minutes + <span class="string">"分"</span> + seconds + <span class="string">"秒"</span>;</span><br><span class="line">        <span class="comment">//打印当前时间</span></span><br><span class="line">        <span class="built_in">console</span>.log(dateTime)</span><br><span class="line">    &#125;</span><br><span class="line">&lt;<span class="regexp">/script&gt;</span></span><br></pre></td></tr></table></figure>

<p>注意</p>
<p>setInterval(Clock, 1000)</p>
<p>setInterval(方法名不用括号)</p>
<p>clearInterval(close_time); 消除定时器</p>
<p>输出input的内容</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"text"</span> <span class="attr">name</span>=<span class="string">""</span> <span class="attr">id</span>=<span class="string">"text"</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"按钮"</span> <span class="attr">id</span>=<span class="string">"btn"</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">    <span class="keyword">var</span> text = <span class="built_in">document</span>.getElementById(<span class="string">"text"</span>);</span></span><br><span class="line"><span class="javascript">    <span class="keyword">var</span> btn = <span class="built_in">document</span>.getE	lementById(<span class="string">"btn"</span>);</span></span><br><span class="line"><span class="actionscript">    btn.onclick = <span class="function"><span class="keyword">function</span> <span class="title">Content</span><span class="params">()</span> </span>&#123;</span></span><br><span class="line">        alert(text.value);</span><br><span class="line">    &#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="js鼠标滚轮事件"><a href="#js鼠标滚轮事件" class="headerlink" title="js鼠标滚轮事件"></a>js鼠标滚轮事件</h3><p><a href="https://blog.csdn.net/u014205965/article/details/46045099" target="_blank" rel="noopener">https://blog.csdn.net/u014205965/article/details/46045099</a></p>
<p>鼠标滚轮事件在 IE和谷歌浏览器Chrome下是通过onmousewheel这个事件实现的，但是火狐FF下却不识别onmousewheel，在FF下需要用DOMMouseScroll，并且必须用“事件监听”方式添加事件才有效；</p>
<p>事件监听                  滚轮事件</p>
<p>谷歌   addEventListener         onmousewheel</p>
<p> IE       attachEvent                 onmousewheel</p>
<p> FF     addEventListener         DOMMouseScroll</p>
<p>Js 生成10个按钮，每个点击弹出1-10</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">var btn = document.createElement('button');</span><br><span class="line">btn.innerText = i;</span><br><span class="line">document.body.appendChild(btn);</span><br></pre></td></tr></table></figure>

<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">var i = 0;</span><br><span class="line">for (i = 1; i <span class="tag">&lt;<span class="name">=</span> <span class="attr">10</span>; <span class="attr">i</span>++) &#123;</span></span><br><span class="line"><span class="tag">     (<span class="attr">function</span> (<span class="attr">i</span>) &#123;</span></span><br><span class="line"><span class="tag">                   <span class="attr">var</span> <span class="attr">btn</span> = <span class="string">document.createElement(</span>'<span class="attr">button</span>');</span></span><br><span class="line"><span class="tag">                   // <span class="attr">createElement</span>('') 创建元素</span></span><br><span class="line"><span class="tag">                   <span class="attr">btn.innerText</span> = <span class="string">i;</span></span></span><br><span class="line"><span class="tag">                   // 给按钮添加内容             </span></span><br><span class="line"><span class="tag">                   <span class="attr">btn.onclick</span> = <span class="string">function</span> () &#123;</span></span><br><span class="line"><span class="tag">                   <span class="attr">alert</span>(<span class="attr">i</span>);</span></span><br><span class="line"><span class="tag">                   &#125;;</span></span><br><span class="line"><span class="tag">                   <span class="attr">document.body.appendChild</span>(<span class="attr">btn</span>);</span></span><br><span class="line"><span class="tag">                   // <span class="attr">appendChild</span>()             </span></span><br><span class="line"><span class="tag">                   &#125;)(<span class="attr">i</span>);</span></span><br><span class="line"><span class="tag"> &#125;</span></span><br></pre></td></tr></table></figure>







<h3 id="js-二十行贪食蛇"><a href="#js-二十行贪食蛇" class="headerlink" title="js 二十行贪食蛇"></a>js 二十行贪食蛇</h3><p><a href="https://www.bilibili.com/video/av35342723?p=6" target="_blank" rel="noopener">https://www.bilibili.com/video/av35342723?p=6</a></p>
<p><a href="https://www.bilibili.com/video/av60721073/" target="_blank" rel="noopener">https://www.bilibili.com/video/av60721073/</a></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!doctype <span class="meta-keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">canvas</span> <span class="attr">id</span>=<span class="string">"can"</span> <span class="attr">width</span>=<span class="string">"400"</span> <span class="attr">height</span>=<span class="string">"400"</span> <span class="attr">style</span>=<span class="string">"background: Black"</span>&gt;</span><span class="tag">&lt;/<span class="name">canvas</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">        <span class="keyword">var</span> sn = [<span class="number">42</span>, <span class="number">41</span>], dz = <span class="number">43</span>, fx = <span class="number">1</span>, n, ctx = <span class="built_in">document</span>.getElementById(<span class="string">"can"</span>).getContext(<span class="string">"2d"</span>);</span></span><br><span class="line"><span class="actionscript">        <span class="function"><span class="keyword">function</span> <span class="title">draw</span><span class="params">(t, c)</span> </span>&#123;</span></span><br><span class="line">            ctx.fillStyle = c;</span><br><span class="line">            ctx.fillRect(t % 20 * 20 + 1, ~~(t / 20) * 20 + 1, 18, 18);</span><br><span class="line">        &#125;</span><br><span class="line"><span class="javascript">        <span class="built_in">document</span>.onkeydown = <span class="function"><span class="keyword">function</span> (<span class="params">e</span>) </span>&#123;</span></span><br><span class="line">            fx = sn[1] - sn[0] == (n = [-1, -20, 1, 20][(e || event).keyCode - 37] || fx) ? fx : n</span><br><span class="line">        &#125;;</span><br><span class="line"><span class="actionscript">        !<span class="function"><span class="keyword">function</span> <span class="params">()</span> </span>&#123;</span></span><br><span class="line">            sn.unshift(n = sn[0] + fx);</span><br><span class="line"><span class="handlebars"><span class="xml">            if (sn.indexOf(n, 1) &gt; 0 || n <span class="tag">&lt; <span class="attr">0</span> || <span class="attr">n</span> &gt;</span> 399 || fx == 1 &amp;&amp; n % 20 == 0 || fx == -1 &amp;&amp; n % 20 == 19)</span></span></span><br><span class="line"><span class="actionscript">                <span class="keyword">return</span> alert(<span class="string">"GAME OVER"</span>);</span></span><br><span class="line"><span class="actionscript">            draw(n, <span class="string">"Lime"</span>);</span></span><br><span class="line">            if (n == dz) &#123;</span><br><span class="line"><span class="javascript">                <span class="keyword">while</span> (sn.indexOf(dz = ~~(<span class="built_in">Math</span>.random() * <span class="number">400</span>)) &gt;= <span class="number">0</span>);</span></span><br><span class="line"><span class="actionscript">                draw(dz, <span class="string">"Yellow"</span>);</span></span><br><span class="line"><span class="actionscript">            &#125; <span class="keyword">else</span></span></span><br><span class="line"><span class="actionscript">                draw(sn.pop(), <span class="string">"Black"</span>);</span></span><br><span class="line"><span class="javascript">            setTimeout(<span class="built_in">arguments</span>.callee, <span class="number">130</span>);</span></span><br><span class="line">        &#125;();</span><br><span class="line">    <span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>



<p>更换属性值</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> myImage = <span class="built_in">document</span>.querySelector(<span class="string">'input'</span>);</span><br><span class="line">myImage.onclick = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    mySrc = myImage.getAttribute(<span class="string">'value'</span>);</span><br><span class="line">    <span class="keyword">if</span> (mySrc === <span class="string">'我是一个按钮'</span>) &#123;</span><br><span class="line">    myImage.setAttribute(<span class="string">'value'</span>, <span class="string">'你好按钮'</span>);</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    myImage.setAttribute(<span class="string">'value'</span>, <span class="string">'我是一个按钮'</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">myImage.onclick = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (myImage.value == <span class="string">"我是一个按钮"</span>) &#123;</span><br><span class="line">    myImage.value = <span class="string">'你好按钮'</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    myImage.value = <span class="string">'我是一个按钮'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>





<p>输入函数</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> myName = prompt(<span class="string">'请输入你的名字。'</span>);</span><br></pre></td></tr></table></figure>





<p>JS轮播事件</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>









<p>pics[i].style.display = “none”;</p>
<p>2048</p>
<p><a href="https://www.bilibili.com/video/av47982145/" target="_blank" rel="noopener">https://www.bilibili.com/video/av47982145/</a></p>
<p>js onput 和onchage事件</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">&lt;div id&#x3D;&quot;div1&quot;&gt;</span><br><span class="line">       &lt;!--非vue--&gt;</span><br><span class="line">       JS的onchage事件：</span><br><span class="line">       &lt;input type&#x3D;&quot;text&quot; id&#x3D;&quot;t1&quot; onchange&#x3D;&quot;p1()&quot;&gt;</span><br><span class="line">       &lt;p id&#x3D;&quot;pp&quot;&gt;原始值&lt;&#x2F;p&gt;</span><br><span class="line">   &lt;&#x2F;div&gt;</span><br><span class="line">   &lt;script&gt;</span><br><span class="line">       function p1() &#123;</span><br><span class="line">           var lb_p &#x3D; document.getElementById(&quot;pp&quot;);</span><br><span class="line">           var txt_t1 &#x3D; document.getElementById(&quot;t1&quot;).value;</span><br><span class="line">           lb_p.innerHTML &#x3D; txt_t1;</span><br><span class="line">       &#125;</span><br><span class="line">   &lt;&#x2F;script&gt;</span><br><span class="line"></span><br><span class="line">   &lt;div id&#x3D;&quot;div2&quot;&gt;</span><br><span class="line">       &lt;!--非vue-一些ie低版本不支持--&gt;</span><br><span class="line">       JS的oninput事件：</span><br><span class="line">       &lt;input type&#x3D;&quot;text&quot; id&#x3D;&quot;t2&quot; oninput&#x3D;&quot;p2()&quot;&gt;</span><br><span class="line">       &lt;h1&gt;原始值+&lt;p id&#x3D;&quot;pp2&quot;&gt;&lt;&#x2F;p&gt;</span><br><span class="line">       &lt;&#x2F;h1&gt;</span><br><span class="line">   &lt;&#x2F;div&gt;</span><br><span class="line">   &lt;script&gt;</span><br><span class="line">       function p2() &#123;</span><br><span class="line">           var lb_p &#x3D; document.getElementById(&quot;pp2&quot;);</span><br><span class="line">           var txt_t2 &#x3D; document.getElementById(&quot;t2&quot;).value;</span><br><span class="line">           lb_p.innerHTML &#x3D; txt_t2;</span><br><span class="line">       &#125;</span><br><span class="line">   &lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>



<p>Js tab</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br></pre></td><td class="code"><pre><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html lang&#x3D;&quot;en&quot;&gt;</span><br><span class="line"></span><br><span class="line">&lt;head&gt;</span><br><span class="line">    &lt;meta charset&#x3D;&quot;UTF-8&quot;&gt;</span><br><span class="line">    &lt;meta name&#x3D;&quot;viewport&quot; content&#x3D;&quot;width&#x3D;device-width, initial-scale&#x3D;1.0&quot;&gt;</span><br><span class="line">    &lt;title&gt;Document&lt;&#x2F;title&gt;</span><br><span class="line"></span><br><span class="line">    &lt;style&gt;</span><br><span class="line">        * &#123;</span><br><span class="line">            margin: 0;</span><br><span class="line">            padding: 0;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        body &#123;</span><br><span class="line">            background: #b2dfdb;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        .tab-control &#123;</span><br><span class="line">            width: 660px;</span><br><span class="line">            height: 340px;</span><br><span class="line">            margin: 100px auto;</span><br><span class="line">            display: flex;</span><br><span class="line">            border-radius: 15px;</span><br><span class="line">            flex-direction: column;</span><br><span class="line">            justify-content: space-between;</span><br><span class="line">            background: gold;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        .tab-item &#123;</span><br><span class="line">            width: 660px;</span><br><span class="line">            height: 40px;</span><br><span class="line">            line-height: 40px;</span><br><span class="line">            background: pink;</span><br><span class="line">            display: flex;</span><br><span class="line">            justify-content: space-around;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        .tab-item div &#123;</span><br><span class="line">            width: 220px;</span><br><span class="line">            text-align: center;</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        .tab-item div:hover &#123;</span><br><span class="line">            cursor: pointer;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        .tab-content &#123;</span><br><span class="line">            width: 660px;</span><br><span class="line">            height: 300px;</span><br><span class="line">            background: #1a237e;</span><br><span class="line">            display: flex;</span><br><span class="line">            justify-content: space-around;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        .tab-content div &#123;</span><br><span class="line">            padding: 30px;</span><br><span class="line">            display: none;</span><br><span class="line">            color: white;</span><br><span class="line">            border-radius: 2px;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        .active &#123;</span><br><span class="line">            background: #d32f2f;</span><br><span class="line">            color: white;</span><br><span class="line">        &#125;</span><br><span class="line">    &lt;&#x2F;style&gt;</span><br><span class="line">&lt;&#x2F;head&gt;</span><br><span class="line"></span><br><span class="line">&lt;body&gt;</span><br><span class="line">    &lt;div class&#x3D;&quot;tab-control&quot;&gt;</span><br><span class="line">        &lt;div class&#x3D;&quot;tab-item&quot;&gt;</span><br><span class="line">            &lt;div class&#x3D;&quot;active&quot;&gt;HTML&lt;&#x2F;div&gt;</span><br><span class="line">            &lt;div&gt;CSS&lt;&#x2F;div&gt;</span><br><span class="line">            &lt;div&gt;Javascript&lt;&#x2F;div&gt;</span><br><span class="line">        &lt;&#x2F;div&gt;</span><br><span class="line">        &lt;div class&#x3D;&quot;tab-content&quot;&gt;</span><br><span class="line">            &lt;div style&#x3D;&quot;display: block;&quot;&gt;超文本标记语言（英语：HyperText Markup</span><br><span class="line">                Language，简称：HTML）是一种用于创建网页的标准标记语言。HTML是一种基础技术，常与CSS、JavaScript一起被众多网站用于设计网页、网页应用程序以及移动应用程序的用户界面[3]。网页浏览器可以读取HTML文件，并将其渲染成可视化网页。HTML描述了一个网站的结构语义随着线索的呈现，使之成为一种标记语言而非编程语言。</span><br><span class="line">            &lt;&#x2F;div&gt;</span><br><span class="line">            &lt;div&gt;层叠样式表 (Cascading Style Sheets，缩写为 CSS），是一种 样式表 语言，用来描述 HTML 或 XML（包括如 SVG、MathML、XHTML 之类的 XML</span><br><span class="line">                分支语言）文档的呈现。CSS 描述了在屏幕、纸质、音频等其它媒体上的元素应该如何被渲染的问题。&lt;&#x2F;div&gt;</span><br><span class="line">            &lt;div&gt;JavaScript ( JS ) 是一种具有函数优先的轻量级，解释型或即时编译型的编程语言。虽然它是作为开发Web 页面的脚本语言而出名的，但是它也被用到了很多非浏览器环境中&lt;&#x2F;div&gt;</span><br><span class="line">        &lt;&#x2F;div&gt;</span><br><span class="line">    &lt;&#x2F;div&gt;</span><br><span class="line">    &lt;script&gt;</span><br><span class="line">        function tab() &#123;</span><br><span class="line">            var tabiteml &#x3D; document.getElementsByClassName(&quot;tab-item&quot;)[0];</span><br><span class="line">            var title &#x3D; tabiteml.getElementsByTagName(&quot;div&quot;);</span><br><span class="line">            var tabcontent &#x3D; document.getElementsByClassName(&quot;tab-content&quot;)[0];</span><br><span class="line">            var content &#x3D; tabcontent.getElementsByTagName(&quot;div&quot;);</span><br><span class="line">            for (let i &#x3D; 0; i &lt; title.length; i++) &#123;</span><br><span class="line">                title[i].index &#x3D; i;</span><br><span class="line">                title[i].onclick &#x3D; function () &#123;</span><br><span class="line">                    for (let j &#x3D; 0; j &lt; title.length; j++) &#123;</span><br><span class="line">                        title[j].className &#x3D; &quot;&quot;;</span><br><span class="line">                        content[j].style.display &#x3D; &quot;none&quot;;</span><br><span class="line">                    &#125;</span><br><span class="line">                    this.className &#x3D; &quot;active&quot;;</span><br><span class="line">                    content[title[i].index].style.display &#x3D; &quot;block&quot;;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        tab();</span><br><span class="line">    &lt;&#x2F;script&gt;</span><br><span class="line">&lt;&#x2F;body&gt;</span><br><span class="line"></span><br><span class="line">&lt;&#x2F;html&gt;</span><br></pre></td></tr></table></figure>



<p>sort()方法（排序）</p>
<p><a href="https://segmentfault.com/a/1190000000410506" target="_blank" rel="noopener">https://segmentfault.com/a/1190000000410506</a></p>
<p><code>sort()</code> 方法用于对数组的元素进行排序,并返回数组。默认排序顺序是根据字符串<code>Unicode</code>码点。</p>
<p>语法：<code>arrayObject.sort(sortby)；参数sortby可选。规定排序顺序。必须是函数。</code></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = <span class="keyword">new</span> <span class="built_in">Array</span>(<span class="number">6</span>)</span><br><span class="line">arr[<span class="number">0</span>] = <span class="string">"George"</span></span><br><span class="line">arr[<span class="number">1</span>] = <span class="string">"John"</span></span><br><span class="line">arr[<span class="number">2</span>] = <span class="string">"Thomas"</span></span><br><span class="line">arr[<span class="number">3</span>] = <span class="string">"James"</span></span><br><span class="line">arr[<span class="number">4</span>] = <span class="string">"Adrew"</span></span><br><span class="line">arr[<span class="number">5</span>] = <span class="string">"Martin"</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">document</span>.write(arr + <span class="string">"&lt;br /&gt;"</span>)</span><br><span class="line"><span class="built_in">document</span>.write(arr.sort())</span><br><span class="line"></span><br><span class="line">George,John,Thomas,James,Adrew,Martin</span><br><span class="line">Adrew,George,James,John,Martin,Thomas</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> arr = <span class="keyword">new</span> <span class="built_in">Array</span>(<span class="number">6</span>)</span><br><span class="line">arr[<span class="number">0</span>] = <span class="string">"10"</span></span><br><span class="line">arr[<span class="number">1</span>] = <span class="string">"5"</span></span><br><span class="line">arr[<span class="number">2</span>] = <span class="string">"40"</span></span><br><span class="line">arr[<span class="number">3</span>] = <span class="string">"25"</span></span><br><span class="line">arr[<span class="number">4</span>] = <span class="string">"1000"</span></span><br><span class="line">arr[<span class="number">5</span>] = <span class="string">"1"</span></span><br><span class="line"><span class="built_in">document</span>.write(arr + <span class="string">"&lt;br /&gt;"</span>)</span><br><span class="line"><span class="built_in">document</span>.write(arr.sort())</span><br><span class="line"></span><br><span class="line"><span class="number">10</span>,<span class="number">5</span>,<span class="number">40</span>,<span class="number">25</span>,<span class="number">1000</span>,<span class="number">1</span></span><br><span class="line"><span class="number">1</span>,<span class="number">10</span>,<span class="number">1000</span>,<span class="number">25</span>,<span class="number">40</span>,<span class="number">5</span> <span class="comment">//第一位数字，第二位数字</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//升序</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">sortNumber</span>(<span class="params">a,b</span>)</span>&#123;</span><br><span class="line">	<span class="keyword">return</span> a - b;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> arr = [<span class="string">"34"</span>,<span class="string">"324"</span>,<span class="string">"67"</span>,<span class="string">"23"</span>];</span><br><span class="line"><span class="built_in">console</span>.log(arr.sort(sortNumber));</span><br><span class="line"><span class="comment">//降序</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">sortNumber</span>(<span class="params">a,b</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> b - a;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> arr = [<span class="string">"34"</span>,<span class="string">"324"</span>,<span class="string">"67"</span>,<span class="string">"23"</span>];</span><br><span class="line"><span class="built_in">console</span>.log(arr.sort(sortNumber));</span><br><span class="line"><span class="comment">//如何根据参数不同，来确定是升序排列，还是降序排序</span></span><br><span class="line">sortBy: <span class="function"><span class="keyword">function</span>(<span class="params">attr,rev</span>)</span>&#123;</span><br><span class="line">    <span class="comment">//第二个参数没有传递 默认升序排列</span></span><br><span class="line">    <span class="keyword">if</span>(rev ==  <span class="literal">undefined</span>)&#123;</span><br><span class="line">        rev = <span class="number">1</span>;</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;d</span><br><span class="line">        rev = (rev) ? <span class="number">1</span> : <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="function"><span class="keyword">function</span>(<span class="params">a,b</span>)</span>&#123;</span><br><span class="line">        a = a[attr];</span><br><span class="line">        b = b[attr];</span><br><span class="line">        <span class="keyword">if</span>(a &lt; b)&#123;</span><br><span class="line">            <span class="keyword">return</span> rev * <span class="number">-1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(a &gt; b)&#123;</span><br><span class="line">            <span class="keyword">return</span> rev * <span class="number">1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">newArray.sort(sortBy(<span class="string">'number'</span>,<span class="literal">false</span>))</span><br></pre></td></tr></table></figure>






    </div>

    
    
    
        <div class="reward-container">
  <div></div>
  <button onclick="var qr = document.getElementById('qr'); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
      
      <div style="display: inline-block;">
        <img src="/images/wechatpay.jpg" alt="heweiliang 微信支付">
        <p>微信支付</p>
      </div>
      
      <div style="display: inline-block;">
        <img src="/images/alipay.jpg" alt="heweiliang 支付宝">
        <p>支付宝</p>
      </div>

  </div>
</div>

        

  <div class="followme">
    <p>欢迎关注我的其它发布渠道</p>

    <div class="social-list">

        <div class="social-item">
          <a target="_blank" class="social-link" href="/atom.xml">
            <span class="icon">
              <i class="fa fa-rss"></i>
            </span>

            <span class="label">RSS</span>
          </a>
        </div>
    </div>
  </div>


      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/04/13/%E5%89%8D%E7%AB%AF/JavaScript/JavaScript%E5%86%85%E7%BD%AE%E5%AF%B9%E8%B1%A1/" rel="prev" title="JavaScript内置对象">
      <i class="fa fa-chevron-left"></i> JavaScript内置对象
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/04/15/%E5%89%8D%E7%AB%AF/JavaScript/JavaScript/%E5%8F%82%E8%80%83%E6%89%8B%E5%86%8C%E5%AD%A6%E4%B9%A0/HTML%20DOM%20%E5%AF%B9%E8%B1%A1/" rel="next" title="前端/JavaScript/JavaScript/参考手册学习/HTML DOM 对象">
      前端/JavaScript/JavaScript/参考手册学习/HTML DOM 对象 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    
  <div class="comments">
    <div id="lv-container" data-id="city" data-uid="MTAyMC81MDY1MC8yNzEzMw=="></div>
  </div>
  

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#JS点击事件的绑定"><span class="nav-number">1.</span> <span class="nav-text">JS点击事件的绑定</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#JS语法实例"><span class="nav-number"></span> <span class="nav-text">JS语法实例</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#js控制按钮内容相互切换"><span class="nav-number">0.1.</span> <span class="nav-text">js控制按钮内容相互切换</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#JS打印时间"><span class="nav-number">0.2.</span> <span class="nav-text">JS打印时间</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#js鼠标滚轮事件"><span class="nav-number">0.3.</span> <span class="nav-text">js鼠标滚轮事件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#js-二十行贪食蛇"><span class="nav-number">0.4.</span> <span class="nav-text">js 二十行贪食蛇</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="heweiliang"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">heweiliang</p>
  <div class="site-description" itemprop="description">heweiliang blog</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">96</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">18</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">24</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/yourname" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;yourname" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:yourname@gmail.com" title="E-Mail → mailto:yourname@gmail.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="https://plus.google.com/yourname" title="Google → https:&#x2F;&#x2F;plus.google.com&#x2F;yourname" rel="noopener" target="_blank"><i class="fa fa-fw fa-google"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="https://stackoverflow.com/yourname" title="StackOverflow → https:&#x2F;&#x2F;stackoverflow.com&#x2F;yourname" rel="noopener" target="_blank"><i class="fa fa-fw fa-stack-overflow"></i></a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title">
      <i class="fa fa-fw fa-link"></i>
      Links
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="http://yoursite.com/" title="http:&#x2F;&#x2F;yoursite.com">Title</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="http://yoursite.com/" title="http:&#x2F;&#x2F;yoursite.com">Title2</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="http://yoursite.com/" title="http:&#x2F;&#x2F;yoursite.com">Title3</a>
        </li>
    </ul>
  </div>

      </div>
        <div class="back-to-top motion-element">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">heweiliang</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    <span title="站点总字数">376k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">5:42</span>
</div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>

<script src="/js/bookmark.js"></script>


  <script defer src="/lib/three/three.min.js"></script>
    <script defer src="/lib/three/three-waves.min.js"></script>
    <script defer src="/lib/three/canvas_lines.min.js"></script>


  




  
<script src="/js/local-search.js"></script>













  

  

<script>
NexT.utils.loadComments(document.querySelector('#lv-container'), () => {
  window.livereOptions = {
    refer: location.pathname.replace(CONFIG.root, '').replace('index.html', '')
  };
  (function(d, s) {
    var j, e = d.getElementsByTagName(s)[0];
    if (typeof LivereTower === 'function') { return; }
    j = d.createElement(s);
    j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
    j.async = true;
    e.parentNode.insertBefore(j, e);
  })(document, 'script');
});
</script>

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginModelPath":"assets/","model":{"jsonPath":"/live2dw/assets/Epsilon2.1.model.json"},"display":{"position":"left","width":300,"height":600},"mobile":{"show":false},"log":false,"pluginJsPath":"lib/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>
